package com.yztc.youbadbad.presenter;

import android.util.Log;

import com.yztc.youbadbad.model.IOverseaModel;
import com.yztc.youbadbad.model.OverseaModelImpl;
import com.yztc.youbadbad.ui.oversea.IOverseaView;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by KIO on 2016/11/17.
 */

public class OverseaPresenterImpl implements IOverseaPresenter {
    private IOverseaModel iOverseaModel=null;
    private IOverseaView iOverseaView=null;

    public OverseaPresenterImpl(IOverseaView iOverseaView) {
        iOverseaModel = new OverseaModelImpl();
        this.iOverseaView = iOverseaView;
    }

    @Override
    public void getOverseaObservable(int country) {

        iOverseaModel.getOverseaObservable(country)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iOverseaView.showProgressBar();
                    }

                    @Override
                    public void onCompleted() {
                        iOverseaView.hideProgressBar();
                    }

                    @Override
                    public void onError(Throwable e) {
                        iOverseaView.showErrorMessage();
                        iOverseaView.hideProgressBar();
                    }

                    @Override
                    public void onNext(String s) {
                        Log.e("KIO","--->"+s);
                        iOverseaView.getOverseaObservable(s);

                    }
                });
    }

    @Override
    public void getOverseaLike(int country) {

        iOverseaModel.getOverseaLike(country)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        iOverseaView.showProgressBar();
                    }

                    @Override
                    public void onCompleted() {
                        iOverseaView.hideProgressBar();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("KIO","--->"+e.getMessage());
                        iOverseaView.showErrorMessage();
                        iOverseaView.hideProgressBar();
                    }

                    @Override
                    public void onNext(String s) {
                        Log.e("KIO","--->"+s);
                        iOverseaView.getOverseaLike(s);

                    }
                });
    }

}
